using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 顺序节点
/// </summary>
public class Sequence : Node
{
    public Sequence() : base(){}
    public Sequence(List<Node> children) : base(children){}

    public override NodeState Evaluate()
    {
        bool anyChildIsRunning = false;
        
        // 遍历所有子节点
        foreach (Node node in children)
        {
            // 将子节点的状态作为参数传递
            switch (node.Evaluate())
            {
                // 如果节点状态执行为失败，那么将该节点状态作为结果返回给父类
                case NodeState.FAILURE:
                    state = NodeState.FAILURE;
                    return state;
                // 如果节点状态执行为成功，那么跳过该次循环，继续执行
                case NodeState.SUCCESS:
                    continue;
                // 执行中
                case NodeState.RUNNING:
                    anyChildIsRunning = true;
                    continue;
                default:
                    state = NodeState.SUCCESS;
                    return state;
            }
        }
        // 默认返回为  执行中 或者是 成功
        state = anyChildIsRunning ? NodeState.RUNNING : NodeState.SUCCESS;
        // 返回节点状态
        return state;
    }
}
